package com.example.ioc;

import com.example.ioc.userController.User;
import com.example.ioc.userController.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "com.example")
@SpringBootApplication
public class J20240323SpringIocApplication {
    public static void main(String[] args) {
        //获取Spring上下文对象
        ApplicationContext context = SpringApplication.run(J20240323SpringIocApplication.class, args);

//        //从Spring上下文中获取对象
//
//        //通过类对象获取
//        UserController userController1 = context.getBean(UserController.class);
//        userController1.say();
//
//        //通过对象名获取
//        UserController userController2 = (UserController)context.getBean("userController");
//        userController2.say();
//
//        //通过类对象和对象名获取
//        UserController userController3 = context.getBean("userController", UserController.class);
//        userController3.say();
//
//        User user1 = (User) context.getBean("getUser1", User.class);
//        user1.say();
//        User user2 = (User) context.getBean("getUser2", User.class);
//        user2.say();
//        System.out.println(user1 == user2);

        UserController userController = context.getBean(UserController.class);
        userController.say();
    }
}
